Skip to content

Fix JNI affinity mask copying and make JNA initialisation more resilient - #192

Open
peter-lawrey wants to merge 1 commit into
developfrom
adv/develop
Open

Fix JNI affinity mask copying and make JNA initialisation more resilient#192
peter-lawrey wants to merge 1 commit into
developfrom
adv/develop

Conversation

@peter-lawrey

@peter-lawrey peter-lawrey commented Nov 21, 2025

Copy link
Copy Markdown
Member

Fix JNI affinity-mask copying: the old setter read a full cpu_set_t even when the Java array was shorter, and getter/setter access through GetByteArrayElements was not released. Zero the native mask and use bounded array-region copies. Failed sched_setaffinity calls throw a Java exception with the thread selector, input length and immediately captured errno.

Read JNA's runtime version through MethodHandles.publicLookup().findStaticGetter(...). This accesses the inherited public field through Native without requiring the JNA module to open its package. At the JNA initialisation boundary, recover from its plain Error for an incompatible native library while explicitly propagating VirtualMachineError, ThreadDeath and AssertionError. Retain fallback for absent JNA and native-loading failures, accurate Linux calling-thread contracts, the small POSIX initialisation adjustment and UTF-8 lock metadata.

The final delta against develop 6a57105d9167fbf66dceea6a1b257df211c56959 is eleven files, +459/-83. The original clock test and required JNA import are restored, and the newly callable timer wrapper is removed. The merged README work from #198 is preserved. The additional POM dependency is test-only jna-jpms:5.17.0; the normal JNA version remains BOM-managed and resolved to 5.5.0 in these runs.

Enabled coverage uses real compiled project classes/native code and fresh JVMs:

  • Short permitted-CPU mask round trip, JNI/JNA parity, empty-mask rejection with errno=22 and unchanged affinity, and 10,000 repeated reads. Mask-changing probes restore and verify the exact original mask in finally.
  • JNI-only probes use -Xcheck:jni without JNA on their classpath. Repeated reads are smoke coverage; they do not detect the old unreleased-array-access defect. Parity runs separately from the checker because old JNA binaries emit their own warnings.
  • Normal JNA selection, actual classpath absence, native loading failure, and an actual incompatible pairing: JNA 5.5.0 Java classes expect native ABI 6.1.0 but receive the fixture's 7.0.2 library. The resulting plain Error is logged and NullAffinity is selected.
  • On Java 9+, load the official jna-jpms artifact from the module path with JNA removed from the classpath. Assert that its package is exported but not open, and that Linux JNA affinity works. Isolated classloaders also check propagation of injected VM, thread-termination and assertion errors.
  • Native test eligibility follows the Linux build's ARM32 (os.arch=arm) exclusion and dontMake. A missing library still fails on eligible hosts. If permitted CPUs require all 128 mask bytes, only the short-mask case skips; parity remains eligible.

History was squashed to 3c92318500f568f37a647de5585b8c823033756c after validation. Its Git tree is identical to tested head 03211b75de04fd05fc000f87542d86694cfdd1df; the source and test results below are unchanged.

Validation on 9 September 2026, Linux amd64, Maven 3.8.7 and GCC 13.3.0, at 03211b75de04fd05fc000f87542d86694cfdd1df:

  • Both new JNA compatibility tests failed against the preceding production code, then passed with this correction using real JNA artifacts.
  • OpenJDK 21.0.12 clean reactor verify: 72 reported cases, 69 passed, three existing skips, no failures/errors/retries. All ten focused cases ran. Checkstyle, native compilation, packaging, Javadocs and licence checks passed.
  • OpenJDK 8u502 focused clean verify: nine passed, one expected module-test skip, no failures/errors/retries.
  • ARM32 eligibility control: set os.arch=arm in both Maven and test JVMs; native compilation is omitted and all four native cases skip without a library. dontMake behaves likewise. An ordinary amd64 Surefire invocation without the library produces four expected assertion failures. These are eligibility controls on amd64, not ARM hardware qualification.
JAVA_HOME=/usr/lib/jvm/java-21-openjdk-amd64 mvn -o -B \
  -Dsurefire.rerunFailingTestsCount=0 clean verify

JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 mvn -o -B -pl affinity \
  -Dtest=AffinityInitializationTest,NativeAffinityIntegrationTest \
  -Dsurefire.rerunFailingTestsCount=0 clean verify

Existing disabled/assumption-guarded legacy tests remain. Other platforms, clock accuracy and the full Java 8 reactor are unqualified; the fixed cpu_set_t capacity is unchanged. The high-CPU-only skip branch was source-reviewed, not exercised on suitable hardware. The native C++ is unchanged from the preceding reviewed head.

@peter-lawrey peter-lawrey changed the title Adv/develop Add AI agent guidance and refine affinity utilities Nov 21, 2025
@peter-lawrey peter-lawrey changed the title Add AI agent guidance and refine affinity utilities Strengthen Java-Thread-Affinity native integration, documentation, and quality gates Nov 26, 2025
@tgd
tgd removed their request for review April 28, 2026 08:59
@peter-lawrey peter-lawrey changed the title Strengthen Java-Thread-Affinity native integration, documentation, and quality gates Fix JNI affinity mask copying and make JNA initialisation more resilient Sep 9, 2026
Bound native array copies and report syscall failures with errno context.
Keep UTF-8 lock metadata and accurate calling-thread affinity contracts.
Handle unavailable or incompatible JNA and use a public version getter
that works with its explicit Java module.

Add enabled native-mask, failure, classpath and module regression coverage.
Align native tests with the ARM32 build exclusion and preserve full-mask
parity testing when no short permitted-CPU mask is available.

Squashed from 03211b7 without changing
the source tree. Linux validation on that tree: Java 21 reactor 69 passed,
three skipped; Java 8 focused nine passed, one module-test skip.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants